package com.linguochao.rabbitmq.controller;

import com.linguochao.rabbitmq.config.RabbitDelayConfig;
import com.linguochao.rabbitmq.producer.RabbitAdminService;
import com.linguochao.rabbitmq.producer.RabbitProducer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author linguochao
 * @Date 2020/7/16 9:52
 */
@RestController
@CrossOrigin
@RequestMapping("/rabbit")
public class RabbitController {

    @Autowired
    private RabbitProducer rabbitProducer;

    /**
     * 直接模式--发送短信
     *
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/sendsms/{mobile}", method = RequestMethod.POST)
    public String sendsms(@PathVariable String mobile) {
        rabbitProducer.sendsms(mobile);
        return "验证码发送成功";
    }


    /**
     * 分列模式--添加用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/adduser/{user}", method = RequestMethod.POST)
    public String adduser(@PathVariable String user) {
        rabbitProducer.adduser(user);
        return "添加用户成功" + user;
    }

    /**
     * 主题模式-- 推送qq,微信消息
     *
     * @param msg
     * @return
     */
    @RequestMapping(value = "/qqwx/{msg}", method = RequestMethod.POST)
    public String qqwx(@PathVariable String msg) {
        rabbitProducer.qqwx(msg);
        return "推送qq,微信消息" + msg;
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 延迟发送发送短信
     *
     * @return
     */
    @RequestMapping(value = "/delaySms/{mobile}", method = RequestMethod.POST)
    public String delaySms(@PathVariable String mobile) {

        //================测试延迟发送=============
        rabbitTemplate.convertAndSend(RabbitDelayConfig.DELAY_EXCHANGE_NAME, RabbitDelayConfig.DELAY_QUEUE_ONE_ROUTING_KEY, mobile);
        //================测试延迟发送=============

        return "验证码发送成功";
    }

    @Value("#{'${spring.rabbitmq.batchQueue}'.split(',')[0]}")
    private String queues;

    @Value("#{'${spring.rabbitmq.batchQueue}'.split(',')[1]}")
    private String queues2;

    @Value("#{'${spring.rabbitmq.batchQueue}'.split(',')[2]}")
    private String queues3;

    @RequestMapping(value = "/batchMq/{msg}", method = RequestMethod.POST)
    public String batchMq(@PathVariable String msg) {

        rabbitProducer.sendBathHash(msg);

        System.out.println("批量消息发送成功");
        return "批量消息发送成功";
    }

    @Autowired
    private RabbitAdminService rabbitAdminService;

    @RequestMapping(value = "/declareQueue", method = RequestMethod.GET)
    public String declareQueue() {

        rabbitAdminService.declareQueue("declareQueueExchange");

        return "声明队列成功";
    }

}
